﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace graphic_class
{
    //定义图形面积抽象类
    abstract class ShapeArea
    {
        public double area;
        public abstract double getArea();
    }

    //定义检查图形是否合格的父类
    public interface Check
    {
        bool isTrue();
    }

    //三角形类
    class Triangle : ShapeArea, Check
    {
        //三边
        public double side1, side2, side3;

        //构造函数
        public Triangle(double s1, double s2, double s3)
        {
            side1 = s1;
            side2 = s2;
            side3 = s3;
        }

        //重构istrue函数
        public new bool isTrue()
        {
            if(side1>0&&side2>0&&side3>0){
                if (side1 + side2 > side3 && side1 + side3 > side2 && side2 + side3 > side1)
                {
                    return true;
                }
                else
                    return false;
            }
            else
                return false;
        }

        //计算面积
        public override double getArea()
        {
            double p = (side1 + side2 + side3) / 2;
            return Math.Sqrt(p * (p - side1) * (p - side2) * (p - side3));
        }
    }

    //矩形类
    class Rectangle : ShapeArea, Check
    {
        //矩形长宽
        public double len, wid;

        //矩形构造函数
        public Rectangle(double length, double width)
        {
            len = length;
            wid = width;
        }

        //重构isTrue函数
        public new bool isTrue()
        {
            if (len > 0 && wid > 0)
                return true;
            else
                return false;
        }

        //计算矩形面积
        public override double getArea()
        {
            return len * wid;
        }
    }

    class Square : Rectangle
    {
        //正方形边长
        public double side;

        //构造函数
        public Square(double a) : base(a, a)
        {
            side = a;
        }

        //重构isTrue函数
        public new bool isTrue()
        {
            if (side > 0)
                return true;
            else
                return false;
        }

        //计算正方形面积
        public override double getArea()
        {
            return side * side;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {   
            //声明随机数
            Random rd = new Random();
            double sum = 0;

            //随机生成10个图形
            for (int i = 0; i < 10; i++)
            {
                int kind = rd.Next(3);
                switch (kind)
                {
                    //假设0为三角形
                    case 0:
                        Triangle tr = new Triangle(rd.Next(10), rd.Next(10), rd.Next(10));
                        //生成的图像合格化
                        while (!tr.isTrue())
                        {
                            tr.side1 = rd.Next(10);
                            tr.side2 = rd.Next(10);
                            tr.side3 = rd.Next(10);
                        }
                        //计算面积
                        tr.area = tr.getArea();
                        sum = sum + tr.area;
                        Console.WriteLine("创造的第" + (i + 1) + "个形状为三角形,三边长为：" + tr.side1 + ',' + tr.side2 + ',' + tr.side3);
                        break;

                    //1为长方形
                    case 1:
                        Rectangle rec = new Rectangle(rd.Next(10), rd.Next(10));
                        //生成的图像合格化
                        while (!rec.isTrue())
                        {
                            rec.len = rd.Next(10);
                            rec.wid = rd.Next(10);
                        }
                        //计算面积
                        rec.area = rec.getArea();
                        sum = sum + rec.area;
                        Console.WriteLine("创造的第" + (i + 1) + "个形状为长方形，长宽为：" + rec.len + ',' + rec.wid);
                        break;

                    //2为正方形
                    case 2:
                        Square s = new Square(rd.Next(10));
                        //生成的图像合格化
                        while (!s.isTrue())
                        {
                            s.side = rd.Next(10);
                        }
                        //计算面积
                        s.area = s.getArea();
                        sum = sum + s.area;
                        Console.WriteLine("创造的第" + (i + 1) + "个形状为正方形，边长为：" + s.side);
                        break;
                    default:
                        break;
                }

            }

            Console.WriteLine("总面积为：" + sum);
            Console.ReadKey();
        }
    }
}
